﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using QzEbook.Common;
using System.Web;

/// <summary>
///Category 的摘要说明
/// </summary>
/// 
namespace QzEbook.AccessObject
{
    public class Category : abstractAccess<Category>
    {
        private int id;
        private int count;
        private string name;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public int Count
        {
            get { return count; }
            set { count = value; }
        }

        public Category()
        {
        }

        protected override Category bindRow(DataRow row)
        {
            Category category = new Category();
            category.Id = (Int32)row["CID"];
            category.Name = row["CName"].ToString();
            category.Count = (Int32)row["CRefs"];
            return category;
        }

        /// <summary>
        /// 选取分类型(更具体);
        /// </summary>
        /// <returns></returns>
        /// 
        public new List<Category> selectList()
        {
            if (HttpContext.Current.Cache["CategoryList"] == null)
            {
                List<Category> list = base.selectList();
                HttpContext.Current.Cache.Insert("CategoryList", list, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
            }
            return HttpContext.Current.Cache["CategoryList"] as List<Category>;
        }

        public static string getName(int index)
        {
            List<Category> list = new Category().selectList();
            foreach (Category category in list)
            {
                if (category.Id == index)
                {
                    return category.Name;
                }
            }
            return String.Empty;
        }

        public static string getLinkHtml(int categoryId)
        {
            return string.Format("<a href=\"list.xhtml?Cid={0}\">{1}</a>", categoryId, Category.getName(categoryId));
        }
        public static string getLinkHtml(int categoryId,int count)
        {
            return string.Format("<a href=\"list.xhtml?Cid={0}\" title=\"{1}\">{1}({2})</a>", categoryId, Category.getName(categoryId),count);
        }

        /// <summary>
        /// 选取分类型
        /// </summary>
        /// <returns></returns>
        protected override DataTable selectTable()
        {
            DbCommand command = CreateCommand("SelectCategory");
            return DataAccess.ExcuteSelectCommand(command);
        }
    }
}